package com.hotel.po.mapper;

import com.hotel.common.utils.DBTools;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class SeasonPriceMapper {

    public List<Map<String, String>> list(Long roomId) {
        String base = "SELECT id, room_id AS roomId, start_date AS startDate, end_date AS endDate, price, created_at AS createdAt FROM room_price_season";
        return roomId == null ? DBTools.queryWithBusiness(base + " ORDER BY id DESC")
                : DBTools.queryWithBusiness(base + " WHERE room_id=? ORDER BY id DESC", roomId);
    }

    public Long insert(Map<String, Object> body) {
        String sql = "INSERT INTO room_price_season(room_id,start_date,end_date,price) VALUES(?,?,?,?)";
        Long roomId = Long.parseLong(body.get("roomId").toString());
        String startDate = body.get("startDate").toString();
        String endDate = body.get("endDate").toString();
        Double price = Double.parseDouble(body.get("price").toString());
        DBTools.updateWithBusiness(sql, roomId, startDate, endDate, price);
        String id = DBTools.querySingle("SELECT LAST_INSERT_ID() AS id");
        return Long.valueOf(id);
    }

    public int update(Map<String, Object> body) {
        String sql = "UPDATE room_price_season SET room_id=?, start_date=?, end_date=?, price=? WHERE id=?";
        Long id = Long.parseLong(body.get("id").toString());
        Long roomId = Long.parseLong(body.get("roomId").toString());
        String startDate = body.get("startDate").toString();
        String endDate = body.get("endDate").toString();
        Double price = Double.parseDouble(body.get("price").toString());
        return DBTools.updateWithBusiness(sql, roomId, startDate, endDate, price, id);
    }

    public int delete(Long id) {
        return DBTools.updateWithBusiness("DELETE FROM room_price_season WHERE id=?", id);
    }
}


